package insight.google.game.era.data;

import insight.google.game.era.bo.GameRoom;
import insight.google.game.era.bo.GameUser;
import insight.google.game.era.core.ChatCenter;
import insight.google.game.era.core.GameCenterManager;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class RoomDataManager extends AppDataManager {

	
	private static final Log log = LogFactory.getLog(RoomDataManager.class);
	
	public RoomDataManager(){
		this.isDetachable = true;
		super.initialize();
	}
	
	public RoomDataManager(TransactionHandler txHandler){
		this.txHandler = txHandler;
		super.initialize();
	}
	
	public RoomDataManager(TransactionHandler txHandler,boolean isDetachable) {
		this.txHandler = txHandler;
		this.isDetachable = isDetachable;
		super.initialize();
	}
//	@Override
//	public boolean addToDataStore(Object o) {
//		try {
//			this.dataStoreMgr.save(o,pManager,this.isDetachable);
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return false;
//	}
	
	public HashMap<String, GameRoom> initAllRoom(){
		GameRoom room = null;
		for (int i = 0; i < 10; i++) {
			room = new GameRoom("Room # " + i);
			this.addToDataStore(room);
		}
		return this.findAll();
	}
	
	public boolean addAllToDataStore(Collection<GameRoom> o) {
		this.dataStoreMgr.saveAll(o,pManager);
		return false;
	}

	@Override
	public HashMap<String, GameRoom> findAll() {
		List<GameRoom> gameRoomList=null;
		try {
			gameRoomList = this.dataStoreMgr.get(GameRoom.class,pManager,this.isDetachable);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		log.debug("Got All room from DadataStore  ---  "+gameRoomList.size());
		HashMap<String, GameRoom> roomMap = new HashMap<String, GameRoom>();
		if(gameRoomList != null){
			for (GameRoom gameRoom : gameRoomList) {
				roomMap.put(gameRoom.getRoomName(), gameRoom);
			}
		}
		return roomMap;
	}
	
	public GameRoom findByName(String roomName) {
		StringBuffer cond = new StringBuffer();
		cond.append("roomName == '" + roomName + "'");
		return getSingleGameRoom(cond.toString());
	}
	
	public GameRoom findById(String roomId) {
		StringBuffer cond = new StringBuffer();
		cond.append("roomId == '" + KeyFactory.stringToKey(roomId) + "'");
		return getSingleGameRoom(cond.toString());
		
	}
	
	public GameRoom findById(Key roomId) {
		StringBuffer cond = new StringBuffer();
		cond.append("roomId == '" + KeyFactory.keyToString(roomId) + "'");
		return getSingleGameRoom(cond.toString());
		
	}

	
	public String findRoomNameById(Key roomId) {
		StringBuffer cond = new StringBuffer();
		cond.append("roomId == '" + roomId + "'");
		GameRoom room = getSingleGameRoom(cond.toString());
		if(room != null){
			return room.getRoomName();
		}
		return null;
	}
	
	private GameRoom getSingleGameRoom(String cond){
		GameRoom room = null;
		List<GameRoom> roomList= null;
		List<GameUser> userList= null;
		GameUserDataManager guDataManager = new GameUserDataManager(this.getTransactionHandler());
		try {
			roomList = new ArrayList<GameRoom>(DataStoreManager.getInstance().getByCondition(GameRoom.class, cond,pManager,this.isDetachable));
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (roomList != null) {
			for (int i = 0; i < roomList.size(); i++) {
				room = (GameRoom)roomList.get(i);
//				room.setUserList(guDataManager.findAllByRoomId(room.getRoomId()));
				break;
			}
		}
		return room;
	}
	
	
	private void setFetchPlan(){
		pManager.getFetchPlan().addGroup("gameLogManager");
		pManager.getFetchPlan().setMaxFetchDepth(3);
	}
	
	
	public ChatCenter findChatCenter(int owner,String roomName){
		StringBuffer cond = new StringBuffer();
		//cond.append("chatCenterOwner == " + owner + "");
		cond.append("roomName == '" + roomName + "'");
		List<ChatCenter> list= null;
		try {
			list = (List<ChatCenter>)DataStoreManager.getInstance().getByCondition(ChatCenter.class, cond.toString(),pManager,this.isDetachable);
			if(list != null){
				for (ChatCenter chatCenter : list) {
					return chatCenter;
				}
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public ChatCenter findChatCenterById(Key chatCenterId){
		return (ChatCenter)this.dataStoreMgr.getById(ChatCenter.class, chatCenterId, this.pManager,this.isDetachable);
	}
	

}
